<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <form action="javascript:;">
        <input type="text" class="user">
        <input type="password" class="psw">
        <button class="login">登入</button>
        <button class="register">注册</button>
    </form>
    <script>
        let persons = [
            { "username": "xiaoming1", "password": "1111" },
            { "username": "xiaoming2", "password": "222" },
            { "username": "xiaoming3", "password": "33333" },
        ]
        let login = document.querySelector('.login');
        let register = document.querySelector('.register');
        let user = document.querySelector('.user');
        let psw = document.querySelector('.psw');
        register.onclick = function () {
            userText = user.value;
            pswText = psw.value;
            let tag = false;
            if (userText.trim() != '' && pswText.trim() != '') {
                for (let val of persons) {
                    if (val.username == userText) {
                        tag = true;
                        break;
                    }
                }
                if (!tag) {
                    persons.push({ "username": userText, "password": pswText });
                    alert('注册成功');
                    user.value = '';
                    psw.value = '';
                }
                else {
                    alert('该账号已注册');
                }
            }
            else {
                alert('账号或密码不能为空');
            }
        }

        login.onclick = function () {
            userText = user.value;
            pswText = psw.value;
            let tag = false;
            if (userText.trim() != '' && pswText.trim() != '') {
                for (let val of persons) {
                    if (userText == val.username) {
                        tag = true;
                        if (val.username == userText && val.password == pswText) {
                            alert('登入成功');
                            user.value = '';
                            psw.value = '';
                            break;
                        }
                        else {
                            alert('账号或密码输入错误');
                        }
                    }
                }
                if (!tag) {
                    alert('该账号未注册');
                }
            }
            else {
                alert('账号或密码不能为空');
            }
        }
    </script>
</body>

</html>